package util;

import bean.GitVersionInfo;
import bean.StudentJob;
import com.mysql.jdbc.Connection;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;

import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Util {
    // 去读Excel的方法readExcel，该方法的入口参数为一个File对象
    public static List<StudentJob> readExcel(String fileName,int jobNumber) {
        // 创建文件输出流
        FileWriter fw = null;
        List<StudentJob> studentJobs=new ArrayList<StudentJob>();
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(fileName);
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            for (int index = 0; index < sheet_size; index++) {
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);
                // sheet.getRows()返回该页的总行数
                for (int i = 1; i < sheet.getRows(); i++) {
                    // sheet.getColumns()返回该页的总列数
                    StudentJob studentJob=new StudentJob();
                    String xuehao="",name="",url="",date="";
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        String cellinfo = sheet.getCell(j, i).getContents();
                        switch (j) {
                            case 1:
                                date=cellinfo.trim();
                                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
                                studentJob.setJobDate(dateFormat.parse(date));
                                break;
                            case 6:
                                name=cellinfo.trim();
                                studentJob.setName(name);
                                break;
                            case 7:
                                xuehao=cellinfo.trim();
                                studentJob.setStudentId(xuehao);
                                break;
                            case 8:
                                url=cellinfo.trim();
                                studentJob.setJobGitAddress(url);
                                break;
                            default:
                                break;
                        }
                    }
                    studentJob.setJobNumber(jobNumber);
                    studentJobs.add(studentJob);
                    }
                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return studentJobs;
    }

//    public void writeExcelByHashMap(HashMap<String, StudentInfo> students){
//        try{
//            OutputStream os=new FileOutputStream("c:\\test.xls");
//            WritableWorkbook wwb = Workbook.createWorkbook(os);
//            WritableSheet ws = wwb.createSheet("test",0);
//
//            Iterator<Map.Entry<String,StudentInfo>> iterator=students.entrySet().iterator();
//            int i=1;
//
//            ws.addCell(new Label(0, 0, "学号"));
//            ws.addCell(new Label(1, 0, "姓名"));
//            ws.addCell(new Label(2, 0, "到课"));
//            ws.addCell(new Label(3, 0, "准时到达"));
//            ws.addCell(new Label(4, 0, "迟到"));
//            ws.addCell(new Label(5, 0, "早退"));
//            ws.addCell(new Label(6, 0, "缺席"));
//            while(iterator.hasNext()){
//                Map.Entry<String,StudentInfo>entry=iterator.next();
//                String xuehao=entry.getKey();
//                StudentInfo studentInfo=entry.getValue();
//                Label label0 = new Label(0, i, xuehao);
//                ws.addCell(label0);
//
//                Label label1 = new Label(1, i, studentInfo.getName());
//                ws.addCell(label1);
//
//                Label label2 = new Label(2, i, String.valueOf(studentInfo.getDaoke()));
//                ws.addCell(label2);
//
//                Label label3 = new Label(3, i, String.valueOf(studentInfo.getZhunshi()));
//                ws.addCell(label3);
//
//                Label label4 = new Label(4, i, String.valueOf(studentInfo.getChidao()));
//                ws.addCell(label4);
//
//                Label label5 = new Label(5, i, String.valueOf(studentInfo.getZaotui()));
//                ws.addCell(label5);
//
//                Label label6 = new Label(6, i, String.valueOf(studentInfo.getQuexi()));
//                ws.addCell(label6);
//
//                i++;
//            }
//
//            wwb.write();
//            wwb.close();
//        }catch(IOException e){
//            e.printStackTrace();
//        } catch (RowsExceededException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (WriteException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }

    public static boolean cloneCode(StudentJob studentJob){
        try {
            Git.cloneRepository().setURI(studentJob.getJobGitAddress())
                    .setDirectory(new File("F:\\studentJob\\" + studentJob.getStudentId())).call();
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/autocheckcodejob";
        String username = "root";
        String password = "root";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static boolean readCommitInfo(StudentJob studentJob){
        try {
            Git git = Git.open(new File("F:\\studentJob\\" + studentJob.getStudentId() + "\\.git"));
            Iterable<RevCommit> gitlog = git.log().call();
            for (RevCommit revCommit : gitlog) {
                String version = revCommit.getName();//版本号
                String name = revCommit.getAuthorIdent().getName();
                revCommit.getAuthorIdent().getEmailAddress();
                Date date = revCommit.getAuthorIdent().getWhen();//时间
                revCommit.getAuthorIdent().getTimeZone();
                studentJob.addGitVersionInfos(new GitVersionInfo(name,version,date));
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
